package com.lanou3g.demo.service.impl;

import com.lanou3g.demo.commons.ResultWrapper;
import com.lanou3g.demo.entity.Account;
import com.lanou3g.demo.mapper.AccountMapper;
import com.lanou3g.demo.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountService implements IAccountService {

    @Autowired
    private AccountMapper mapper;


    @Override
    public List<Account> findAll() {
        return mapper.findAll();
    }

    @Override
    public Integer addOne(Account account) {
        return mapper.addOne(account);
    }

    @Override
    public Integer upStatus(Integer accountId) {
        return mapper.upStatus(accountId);
    }

    @Override
    public Integer deleteById(Integer accountId) {
        return mapper.deleteById(accountId);
    }

    @Override
    public Account findById(Integer accountId) {
        return mapper.findById(accountId);
    }

    @Override
    @Transactional

    public int update(Account account) {
        return mapper.update(account);
    }

    @Override

    public List<Account> search(Account account) {
        List<Account> search = mapper.search(account);
        return search;
    }

    @Override
    public ResultWrapper findPages(int page, int count) {
        try {
            //计算分页的两个值 page
            int offset = (page - 1) * count;
            // 查询数据总数
            int totalCount = mapper.findTotalCount();
            // 查询需要的数据
            Map<String,Object> map = new HashMap<>();
            map.put("offset",offset);
            map.put("count",count);
            List<Account> items = mapper.findLimit(map);
            // 处理异常
            return ResultWrapper.sucess(totalCount, items);
        } catch (Exception e) {
            e.printStackTrace();
            return  ResultWrapper.error(406,"查询的数据有瑕疵");
        }
    }
}